﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmallERP.DAL;
using SmallERP.MODEL.FoundationModel;
using Smallerp.Contracts;
using System.Data;

namespace SmallERP.Bll.PrintManage
{
    public  class PaperWasteType
    {
        IOperateDB _OperateDb;
        public PaperWasteType(IOperateDB inOp)
        {
            _OperateDb = inOp;
        }
        /// <summary>
        /// 获取纸浪费类型列表
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteType_info> GetPaperWasteTypeInfoList()
        {
            List<PaperWasteType_info> list = new List<PaperWasteType_info>();
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    List<SmallERP.DAL.PaperWasteType> paperWasteTypeList = entities.PaperWasteType.ToList();
            //    foreach (var item in paperWasteTypeList)
            //    {
            //        PaperWasteType_info pWasteTypeInfo = new PaperWasteType_info();
            //        pWasteTypeInfo.Id = item.Id;
            //        pWasteTypeInfo.PaperOperationTypeName = item.PaperOperationTypeName;
            //        list.Add(pWasteTypeInfo);
            //    }
            //}
            string QueryWaste = "select * from PaperWasteType";
            DataTable table = _OperateDb.QueryDataTable(QueryWaste);
            for (int i = 0; i < table.Rows.Count;i++)
            {
                PaperWasteType_info pwti = new PaperWasteType_info();
                pwti.Id = Convert.ToInt32(table.Rows[i]["Id"]);
                pwti.PaperOperationTypeName = Convert.ToString(table.Rows[i]["PaperOperationTypeName"]);

                list.Add(pwti);
            }
                return list;
        }

        /// <summary>
        /// 获取纸浪费类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PaperWasteType_info GetPaperWasteTypeInfo(string name)
        {
            
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{

            //    SmallERP.DAL.PaperWasteType pwastetype = entities.PaperWasteType.FirstOrDefault(p=>p.PaperOperationTypeName==name);
            //    if (pwastetype == null)
            //    {
            //        int ret = Insert(name);
            //        pwastetype = entities.PaperWasteType.FirstOrDefault(p => p.PaperOperationTypeName == name);
            //        if (ret == -1)
            //        {
            //            return null;
            //        }
            //    }
                
                
            //    pWasteInfo.Id = pwastetype.Id;
            //    pWasteInfo.PaperOperationTypeName = pwastetype.PaperOperationTypeName;
                
            //}
            string QueryWaste = "select * from PaperWasteType where PaperOperationTypeName='"+name+"'";
            DataTable table = _OperateDb.QueryDataTable(QueryWaste);
            PaperWasteType_info pwti = new PaperWasteType_info();
            if (table.Rows.Count > 0)
            {
                pwti.Id = Convert.ToInt32(table.Rows[0]["Id"]);
                pwti.PaperOperationTypeName = Convert.ToString(table.Rows[0]["PaperOperationTypeName"]);
            }
            else
            {
                pwti = null;
            }

            return pwti;
        }

        /// <summary>
        /// 插入纸浪费类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public int Insert(string name)
        {
            int ret = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        SmallERP.DAL.PaperWasteType pwasteType = new SmallERP.DAL.PaperWasteType();
            //        pwasteType.PaperOperationTypeName = name;
            //        entities.AddToPaperWasteType(pwasteType);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}

            string insertSql = "insert into PaperWasteType(PaperOperationTypeName) valuesm(" + name + ")"; ;
            ret = _OperateDb.ExecuteNonQuery(insertSql);
            return ret;
        }
    }
}
